package cn.gtv.sdk.dcas.api;

import android.content.Context;
import android.net.Uri;

import com.sad.jetpack.v1.datamodel.api.IDataModelChainInput;
import com.sad.jetpack.v1.datamodel.api.IDataModelInterceptorInput;
import com.sad.jetpack.v1.datamodel.api.IDataModelRequest;

import org.json.JSONObject;

public class HttpDCASObtainEncryptionKeyClient extends BaseHttpDCASDataClient<HttpDCASObtainEncryptionKeyClient>{
    protected HttpDCASObtainEncryptionKeyClient(Context context) {
        super(context);
    }

    public static HttpDCASObtainEncryptionKeyClient newInstance(Context context){
        return new HttpDCASObtainEncryptionKeyClient(context);
    }

    @Override
    public IDataModelInterceptorInput<String> protocolInterceptorInput(String tagAndClientKey) {
        return new IDataModelInterceptorInput<String>() {
            @Override
            public void onInterceptedInput(IDataModelChainInput<String> chainInput) throws Exception {
                IDataModelRequest request=chainInput.request();
                IDataModelRequest.Creator requestCreator=request.toCreator();
                String url= request.url();
                String ts=(System.currentTimeMillis()/1000)+"";
                String signature=SignatureV2Util.createSignatureV2(Uri.parse(url).getPath(),ts,ts);
                JSONObject newJson=CommonRequestData.createJson(ts,signature);
                requestCreator.body(newJson.toString());
                chainInput.proceed(request,null,chainInput.currIndex());
            }
        };
    }
}
